﻿using System;
using System.IO;
using PSE.Deployment.Engine.Interfaces;
using PSE.Deployment.SetupBase;
using PSE.Deployment.SetupBase.Configuration;
using PSE.Deployment.SetupBase.Parameter.Enums;
using PSE.Deployment.SetupBase.Steps;
using Sinacor.Deployment.Setup.Main;

namespace Sinacor.Deployment.Setup.Install.Steps
{
    public class RegisterBoostFile : SetupStep<PSE.Deployment.SetupBase.Configuration.Actions.Copy>
    {
        public RegisterBoostFile(IStepGroup parent, Group group, PSE.Deployment.SetupBase.Configuration.Actions.Copy action, SetupMode setupMode)
            : base(parent, group, action, setupMode)
        {
            this.ResetProgress(1);
        }

        public override void Execute()
        {
            this.ResetProgress(1);

            if (this.SetupMode == SetupMode.Install)
            {
                CopyFile();
            }
            else if (this.SetupMode == SetupMode.Uninstall)
            {
                UninstallArtefacts();
            }
        }

        public override void Undo()
        {
            if (this.SetupMode == SetupMode.Install)
            {
                UninstallArtefacts();
            }
        }

        private void CopyFile()
        {
            string fileName = @"\boost_thread-vc80-mt-1_39.dll";
            string fileNameSource = SetupMainFactory.GetInstance<SinacorMain>().Parameter.InstallationPath + @"Sinacor\ExternalRef" + fileName;
            string fileNameDestination = System.Environment.GetFolderPath(Environment.SpecialFolder.System) + fileName;

            this.IncrementProgress(fileNameDestination);

            if (System.IO.File.Exists(fileNameSource) && !System.IO.File.Exists(fileNameDestination))
            {
                System.IO.File.Copy(fileNameSource, fileNameDestination, true);

                this.AddUsedBy(fileNameDestination);
            }
        }

        protected override void UninstallArtefact(string artefactName)
        {
            if (File.Exists(artefactName))
                File.Delete(artefactName);
        }
    }
}
